package test;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wd.mqtt.BootNettyMqttChannelInboundHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

public class Server {


    public static void main(String[] args) {

//        System.out.println(10001/10.0-1000);
//
//        byte[] sss = new byte[]{3,-14};
//
//        int result = ((sss[0] & 0xFF) << 8) | (sss[1] & 0xFF);
//
//        System.out.println(result&255);

        GBT32960Serversss gbt32960Serversssl = new GBT32960Serversss();
        gbt32960Serversssl.deviceDataToProtocol(ByteBufUtil.decodeHexDump("" +
"232302fe31303030544b45584332333030303031350101511808130f0e0601ff020107f828fa01901804b08018ffff988525010500065b1f9001c1d5d60601000cfc011a0cfa01012001081d0700000000ff000000000801011804b08000780000000cfc0cfc0cfc0cfc0cfc0cfb0cfc0cfc0cfc0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfc0cfb0cfa0cfb0cfa0cfb0cfc0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfc0cfc0cfc0cfc0cfc0cfc0cfb0cfb0cfc0cfc0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfc0cfb0cfb0cfb0cfb0cfa0cfc0cfc0cfc0cfb0cfc0cfc0cfc0cfc0cfc0cfb0cfb0cfb0cfb0cfc0cfc0cfb0cfb0cfb0cfc0cfc0cfc0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfb0cfc0cfb0cfb0cfb0cfc0cfb0cfb0cfc0cfb0cfb0cfb0cfb0cfb0901010012201e1e1f1e201f1d1e1e1e1f201d1e1e1e1f1d"+
                ""
        ));

        System.out.println("end");
        try {
            NioEventLoopGroup bossGroup;

            NioEventLoopGroup workGroup;


            bossGroup = new NioEventLoopGroup(1);
            workGroup = new NioEventLoopGroup();

            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workGroup);
            bootstrap.channel(NioServerSocketChannel.class);

            bootstrap.option(ChannelOption.SO_REUSEADDR, true)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .option(ChannelOption.SO_RCVBUF, 10485760);

            bootstrap.childOption(ChannelOption.TCP_NODELAY, true)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                protected void initChannel(SocketChannel ch) {
                    ChannelPipeline channelPipeline = ch.pipeline();
                    // 设置读写空闲超时时间
                    channelPipeline.addLast(new ByteArrayEncoder());
                    channelPipeline.addLast(new ProcessServerHandler(new GBT32960Serversss()));

                }
            });
            int port = 10002;

            ChannelFuture f = bootstrap.bind(port).sync();
            if(f.isSuccess()){
                System.out.println("startup success port = " + port);
                f.channel().closeFuture().sync();
            } else {
                System.out.println("startup fail port = " + port);
            }


//			//	绑定端口,监听服务是否启动
//			bootstrap.bind(port).addListener((ChannelFutureListener) channelFuture -> {
//				if (channelFuture.isSuccess()) {
//					System.out.println("startup success --- ");
//				} else {
//					System.out.println("startup fail ---");
//				}
//			});
        } catch (Exception e) {
            System.out.println("start exception"+e.toString());
        }

    }
}

 class  ProcessServerHandler  extends ChannelInboundHandlerAdapter{

    private GBT32960Serversss gbt32960Serversssl;


     public ProcessServerHandler(GBT32960Serversss gbt32960Serversssl) {
         this.gbt32960Serversssl = gbt32960Serversssl;
     }

     @Override
     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
         ByteBuf buf = (ByteBuf) msg;
         byte[] bytes = new byte[buf.readableBytes()];

         System.out.println(bytes);
         buf.readBytes(bytes);
         JSONArray jsonObject = (JSONArray) gbt32960Serversssl.deviceDataToProtocol(bytes);
         System.out.println(jsonObject);
         String replay =  jsonObject.getJSONObject(0).getString("replay");
         byte[] ss = ByteBufUtil.decodeHexDump(replay);
         ctx.writeAndFlush(ss);

         System.out.println(ss);

     }

     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
         super.channelActive(ctx);
     }
 }
